package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.base.BaseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.vos.WmNewsVo;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements BaseResult {
    @Autowired
    private WmNewsService wmNewsService;

    @GetMapping("update/status")
    public Boolean updateStatus(@RequestParam Long newsId,@RequestParam Short status){
        if (newsId == null || status == null)return false;
        return wmNewsService.update(Wrappers.<WmNews>lambdaUpdate().eq(WmNews::getId,newsId).set(WmNews::getStatus,status));
    }

    @PostMapping("update/news")
    public Boolean updateNews(@RequestBody WmNews wmNews){
        if (wmNews == null || wmNews.getId() == null)return false;
        return wmNewsService.updateById(wmNews);
    }

    @GetMapping("update/articleId")
    public Boolean updateArticleId(@RequestParam Long newsId,@RequestParam Long articleId){
        if (newsId == null || articleId == null)return false;
        return wmNewsService.update(Wrappers.<WmNews>lambdaUpdate().eq(WmNews::getId,newsId).set(WmNews::getArticleId,articleId));
    }

    @PostMapping("/list")
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto){
        return wmNewsService.findAll(wmNewsPageReqDto);
    }

    @PostMapping("/submit")
    public ResponseResult summitNews(@RequestBody WmNewsDto wmNews){
        return wmNewsService.saveNews(wmNews);
    }

    @GetMapping("/one/{id}")
    public ResponseResult findWmNewsById(@PathVariable("id") Long id) {
        return wmNewsService.findWmNewsById(id);
    }

    @GetMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable("id") Long id) {
        return wmNewsService.delNews(id);
    }

    @GetMapping("down/up/{id}/{enable}")
    public ResponseResult downOrUp(@PathVariable Long id, @PathVariable Short enable){
        return wmNewsService.downOrUp(id,enable);
    }

    @GetMapping("/findRelease")
    public List<Long> findRelease() {
        return wmNewsService.findRelease();
    }

    @PostMapping("/findList")
    public ResponseResult findList(@RequestBody NewsAuthDto dto){
        return wmNewsService.findList(dto);
    }

    @GetMapping("/find_news_vo/{id}")
    public WmNewsVo findWmNewsVo(@PathVariable("id") Long id) {
        return wmNewsService.findWmNewsVo(id);
    }
}
